package ynu.edu.ky_elm_springboot3.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import ynu.edu.ky_elm_springboot3.po.DeliveryAddress;
import ynu.edu.ky_elm_springboot3.service.DeliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Tag(name = "配送地址相关接口")
@RestController
@RequestMapping("/DeliveryAddressController")
public class DeliveryAddressController {

    @Autowired
    private DeliveryAddressService deliveryAddressService;
    
    @Operation(summary = "查询用户配送地址")
    @GetMapping("/listDeliveryAddressByUserId")
    public List<DeliveryAddress> listDeliveryAddressByUserId(@RequestParam("userId") String userId) {
        return deliveryAddressService.listDeliveryAddressByUserId(userId);
    }
    
    @Operation(summary = "根据ID查询配送地址")
    @GetMapping("/getDeliveryAddressById")
    public DeliveryAddress getDeliveryAddressById(@RequestParam("daId") Integer daId) {
        return deliveryAddressService.getDeliveryAddressById(daId);
    }
    
    @Operation(summary = "新增配送地址")
    @PostMapping("/saveDeliveryAddress")
    public int saveDeliveryAddress(@RequestParam("contactName") String contactName,
                                 @RequestParam("contactSex") Integer contactSex,
                                 @RequestParam("contactTel") String contactTel,
                                 @RequestParam("address") String address,
                                 @RequestParam("userId") String userId) {
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        deliveryAddress.setContactName(contactName);
        deliveryAddress.setContactSex(contactSex);
        deliveryAddress.setContactTel(contactTel);
        deliveryAddress.setAddress(address);
        deliveryAddress.setUserId(userId);
        return deliveryAddressService.saveDeliveryAddress(deliveryAddress);
    }
    
    @Operation(summary = "更新配送地址")
    @PutMapping("/updateDeliveryAddress")
    public int updateDeliveryAddress(@RequestParam("daId") Integer daId,
                                   @RequestParam("contactName") String contactName,
                                   @RequestParam("contactSex") Integer contactSex,
                                   @RequestParam("contactTel") String contactTel,
                                   @RequestParam("address") String address) {
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        deliveryAddress.setDaId(daId);
        deliveryAddress.setContactName(contactName);
        deliveryAddress.setContactSex(contactSex);
        deliveryAddress.setContactTel(contactTel);
        deliveryAddress.setAddress(address);
        return deliveryAddressService.updateDeliveryAddress(deliveryAddress);
    }
    
    @Operation(summary = "删除配送地址")
    @DeleteMapping("/removeDeliveryAddress")
    public int removeDeliveryAddress(@RequestParam("daId") Integer daId) {
        return deliveryAddressService.removeDeliveryAddress(daId);
    }
} 